package net.tngou.qcc;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import net.tngou.lucene.Field;
import net.tngou.lucene.Field.Type;
import net.tngou.lucene.TngouDBHelp;

public class MainTest {

	
	
	 public static void main(String[] args) {
		 System.err.println("**************开始******************");
		 String Dir = System.getProperty("user.dir");
		 System.out.println("读取：'"+Dir+"\\input"+"'目录下个的源文件");
		 File filedir = new File(Dir+"\\input");
		 File[] files = filedir.listFiles();
		 System.err.println("找到"+files.length+"个文件！");
		 for (File file : files) {
			String path = file.getPath();
			String name = file.getName();
			System.err.println("准备读取："+path);
			 List<LinkedHashMap<String, String>> list =ExcleUtil.readExcel(path);
			 if(list==null||list.isEmpty())
			 {
				 System.err.println("原始文件错误！");
				 return;
			 }
			 System.err.println("读取源文件："+list.size()+" 行数据");
			 String keyname=null;
			
			 LinkedHashMap<String, String> map = list.get(0);
			 Set<String> keys = map.keySet();
			 keyname = getName(keys);
			 if(keyname==null)
				 {
					
					 System.err.println("没有找到处理文件名称！");
					 return;
			}
				

			 String outpath = Dir+"\\output\\"+name;
			 ExcleUtil.writeExcel(list,outpath,keyname);
			 System.err.println("写入文件："+outpath);
			 file.delete();
			 System.err.println("**************完成******************");
			
		}
		 
	  
	 }
	 
	 
	 public static String getName(Set<String> keys) {
		 for (String e : keys) {
			if(e.indexOf("名称")>=0)return e;
			if(e.toLowerCase().indexOf("name")>=0)return e;
				
		}
		 return null;
	}
	 

}
